<!-- 编辑弹窗 -->
<template>
    <div class="ele-body">
      <stockLogSearch @search="reload"  />
      <el-card>
        <ele-pro-table ref="table"  row-key="id" :columns="warehouseItemColumns"  :datasource="datasource"  :need-page="true"   :height="tableHeight" :full-height="tableFullHeight">
          <!-- 金额 -->
          <template slot="amount" slot-scope="{ row }">
            ￥{{ row.amount }}
          </template>
          <!-- 金额 -->
          <template slot="price" slot-scope="{ row }">
            ￥{{ row.price }}
          </template>
          <!-- 操作列 -->
          <template slot="action"  >
            <el-link type="info"  :underline="false"  icon="el-icon-more" >日志</el-link>
          </template>
          <!-- 单价 -->
          <template slot="billNo" slot-scope="{ row }">
            <el-link type="primary" v-show="row.status!=5"  :underline="false"    @click="clickOrderNo(row)" >{{ row.billNo }}</el-link>
          </template>
        </ele-pro-table>
      </el-card>
    </div>
  </template>
  <script>

  //初始化对像
  import stockLogSearch from '../components/stocklog-search.vue';
  import { removePageTab, reloadPageTab,setPageTabTitle } from '@/utils/page-tab-util';
  import { QueryStockItemLogByCondition } from '@/api/inventory/stockitemlog';
  export default {
    name: 'StockLog',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    components:{stockLogSearch},
    data() {
      return {
        // 表单数据
        form: {},
        fixedHeight:true,
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: 3,
        direction:0,
        productId:'',
        wareHouseId:'',
        // 表格列配置
        warehouseItemColumns: [
          {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
          {prop: 'billNo', label: '单据编号',slot:'billNo', width: 120,showOverflowTooltip: true, align: 'center',},
          {prop: 'billTypeDesc', label: '单据类型',width: 150, showOverflowTooltip: true, align: 'center',},
          {
            prop: 'billDate', label: '单据日期', width: 120,showOverflowTooltip: true, align: 'center',
            formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
          },
          {prop: 'directionDesc', label: '业务方向',width: 100,showOverflowTooltip: true, align: 'center',},
          {
            prop: 'peddingDate', label: '过账日期', width: 120,showOverflowTooltip: true, align: 'center',
            formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD HH:mm");}
          },
          {prop: 'productName', label: '产品名称',width: 120, showOverflowTooltip: true, align: 'center',},
          {prop: 'productCode', label: '产品编号',width: 120, showOverflowTooltip: true, align: 'center',},
          {prop: 'qty', label: '数量',width: 120, showOverflowTooltip: true,align: 'center',},
          {prop: 'price', label: '成本价', width: 150, showOverflowTooltip: true, align: 'center',},
          {prop: 'amount', label: '金额',width: 150,  showOverflowTooltip: true, align: 'center',},
          {prop: 'unitDesc', label: '单位',width: 100, showOverflowTooltip: true, align: 'center',},
          {prop: 'wareHouseDesc', label: '仓库',width: 150, showOverflowTooltip: true, align: 'center',},
          {prop: 'supplierDesc', label: '供应商', width: 150,showOverflowTooltip: true, align: 'center',},
        ],
      };
    },
    computed: {
      // 表格高度
      tableHeight() {
        var fh = this.fixedHeight ? 'calc(100vh - 250px)' : void 0;
        return fh;
      },
      // 表格全屏时高度
      tableFullHeight() {
        var fh =  this.fixedHeight ? 'calc(100vh - 250px)' : null;
        return fh;
      },
    },
    //初始化数据
    mounted(){
      // this.reload();
    },
    created(){
      this.direction = this.$route.query.direction;
      this.productId = this.$route.query.productId;
      this.wareHouseId = this.$route.query.wareHouseId;

      if(this.direction == 2)  setPageTabTitle('出库明细');
    },
    methods: {

      /* 表格数据源 */
      async datasource(params) {

        params.where.pageindex = params.page;
        params.where.pagesize = params.limit;

        //添加排序
        if(params.order.order == null || params.order.order == undefined){
          params.where.orderBys = [{ sort: "createDate", order: 1, }];
        }else{
          params.where.orderBys = [];
          params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
        }

        if(!params.where.conditions)params.where.conditions = {};

        if(this.productId){
          params.where.conditions.productId = this.productId;
        }

        if(this.wareHouseId){
          params.where.conditions.wareHouseId = this.wareHouseId;
        }

        if(this.direction){
          params.where.conditions.direction = this.direction;
        }

        let data = await QueryStockItemLogByCondition(params.where);

        return {
            list:data.items,
            count:data.totalCount,
        };
      },

      /* 刷新表格 */
      reload(where) {
        if(this.$refs.table){
          this.$refs.table.reload({ where: where });
        }
      },

      /* 样式 */
      cellStyle(param){
        const { row, column, rowIndex, columnIndex } = param;
        if(columnIndex==9||columnIndex==10||columnIndex==8){
          return{
            color:'var(--color-primary)'
          }
        }
      },

      clickOrderNo(row){

        // 采购单
        if(row.billType == 0){
          
          removePageTab({ key: '/businesshandling/purordermanager' });
          this.$nextTick(() => {
            this.$router.push({path: '/businesshandling/purordermanager', query: { "billNo": row.billNo } });
          });

          // 入库单
        }else  if(row.billType == 1){ 
          
          removePageTab({ key: '/invoicingmanager/instockmanager' });
          this.$nextTick(() => {
            this.$router.push({path: '/invoicingmanager/instockmanager', query: { "billNo": row.billNo } });
          });
          
          // 销售出库单
        }else  if(row.billType == 2){
          
          removePageTab({ key: '/businesshandling/saleoutmanger' });
          this.$nextTick(() => {
            this.$router.push({path: '/businesshandling/saleoutmanger', query: { "orderNo": row.billNo } });
          });
          
          // 调拨入库  调拨出库
        }else  if(row.billType == 3 || row.billType == 4){
          
          removePageTab({ key: '/invoicingmanager/swapordermanager' });
          this.$nextTick(() => {
            this.$router.push({path: '/invoicingmanager/swapordermanager', query: { "checkNo": row.billNo } });
          });
      
          // 盘赢单 盘亏单
        }else  if(row.billType == 5 || row.billType == 6){
          
          removePageTab({ key: '/invoicingmanager/checkordermanager' });
          this.$nextTick(() => {
            this.$router.push({path: '/invoicingmanager/checkordermanager', query: { "swapNo": row.billNo } });
          });
          
          // 销售订单
        }else  if(row.billType == 7){
          
          removePageTab({ key: '/businesshandling/saleordermanager' });
          this.$nextTick(() => {
            this.$router.push({path: '/businesshandling/saleordermanager', query: { "orderNo": row.billNo } });
          });
          
        }
      },
  },
    
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data && this.data.id) {
            //赋值this.form
            this.form = this.data;
            this.isUpdate = this.data.id !=null && this.data.id !=undefined;
            this.reload();
          } 
        } 
      }
    }
  };
  </script>